package org.chench.extra.logsift.service.impl;

import org.chench.extra.logsift.config.properties.AppProperties;
import org.chench.extra.logsift.enums.ForwardType;
import org.chench.extra.logsift.service.LogHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

/**
 * 日志处理器上下文
 * @author chench
 * @date 2025.05.23
 */
@Service
public class LogHandlerContext implements LogHandler {
    private static final Logger logger = LoggerFactory.getLogger(LogHandlerContext.class);

    private AppProperties appProperties;
    private ConsoleHandlerImpl consoleHandler;
    private FeiShuHandlerImpl feiShuHandler;
    private HttpHandlerImpl httpHandler;
    private WebHandlerImpl webHandler;

    @Override
    public void handleLog(String logAppName, Path filePath, Map<String, Object> logData, String line) {
        List<ForwardType> forwardTypes = this.appProperties.getForwardTypes(logAppName);
        for (ForwardType forwardType : forwardTypes) {
            switch (forwardType) {
                case CONSOLE:
                    this.consoleHandler.handleLog(logAppName, filePath, logData, line);
                    break;
                case FEISHU:
                    this.feiShuHandler.handleLog(logAppName, filePath, logData, line);
                    break;
                case HTTP:
                    this.httpHandler.handleLog(logAppName, filePath, logData, line);
                    break;
                case WEB:
                    this.webHandler.handleLog(logAppName, filePath, logData, line);
                    break;
                default:
                    logger.warn("Unknown forward type:{} ! logAppName:{}", forwardType.getType(), logAppName);
            }
        }
    }

    @Autowired
    public void setAppProperties(AppProperties appProperties) {
        this.appProperties = appProperties;
    }

    @Autowired
    public void setConsoleHandler(ConsoleHandlerImpl consoleHandler) {
        this.consoleHandler = consoleHandler;
    }

    @Autowired
    public void setFeiShuHandler(FeiShuHandlerImpl feiShuHandler) {
        this.feiShuHandler = feiShuHandler;
    }

    @Autowired
    public void setHttpHandler(HttpHandlerImpl httpHandler) {
        this.httpHandler = httpHandler;
    }

    @Autowired
    public void setWebHandler(WebHandlerImpl webHandler) {
        this.webHandler = webHandler;
    }
}